package collection;

import java.util.*;

/**
 * 测试迭代器遍历List   Set   Map
 */
public class TestDiedaiqi {

    public static void main(String[] args) {
        testIteratorList();
        testIteratorSet();
        testIteratorMap();
    }

    //遍历List
    public static void testIteratorList(){
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

        for (Iterator<String> iter=list.iterator();iter.hasNext();){//获取一个迭代器对象
            String temp =  iter.next();
            System.out.println(temp);
        }

    }

    //遍历Set
    public static void testIteratorSet(){
        Set<String> set = new HashSet<>();
        set.add("a");
        set.add("b");
        set.add("c");

        for (Iterator<String> iter=set.iterator();iter.hasNext();){//获取一个迭代器对象
            String temp =  iter.next();
            System.out.println(temp);
        }

    }

    //遍历Map
    public static void testIteratorMap(){
       Map<Integer,String> map1=new HashMap<>();
       map1.put(100,"aaa");
        map1.put(1,"sss");
        map1.put(2,"dd");

        //第一种遍历方式
        Set<Map.Entry<Integer,String>> mm= map1.entrySet();//返回一个键值对的集合
        for (Iterator<Map.Entry<Integer,String>> iter = mm.iterator();iter.hasNext();){
            Map.Entry<Integer,String> temp = iter.next();
            System.out.println(temp.getKey()+"---"+temp.getValue());
            System.out.println(temp);
        }

        System.out.println("*******************************");

        //第二种遍历方式
        Set<Integer> keySet = map1.keySet();
        for(Iterator<Integer> iter=keySet.iterator();iter.hasNext();){
            Integer key = iter.next();
            System.out.println(key+"---"+map1.get(key));
        }
    }
}
